package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.domain.R;
import com.store.domain.dto.UpdatePasswordDto;
import com.store.domain.dto.UserDto;
import com.store.domain.entity.User;
import com.store.domain.vo.LoginUser;
import com.store.mapper.UserMapper;
import com.store.service.UserService;
import com.store.utils.BeanCopyUtils;
import com.store.utils.JwtUtil;
import com.store.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 用户表(User)表服务实现类
 *
 * @author yu
 * @since 2024-06-26 01:12:42
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public R getUserInfo() {
        Integer userId = ThreadLocalUtil.getLocal();
        User user = getById(userId);
        return R.success(user);
    }

    @Override
    public R getPageUser(Integer pageNum, Integer pageSize, UserDto userDto) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(userDto.getUsername() != null, User::getUsername, userDto.getUsername());
        lambdaQueryWrapper.eq(userDto.getStatus() != null, User::getStatus, userDto.getStatus());
        lambdaQueryWrapper.between(userDto.getStartTime() != null && userDto.getEndTime() != null, User::getRegisterTime, userDto.getStartTime(), userDto.getEndTime());
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, lambdaQueryWrapper);
        return R.success(page.getTotal(), page.getRecords());
    }

    @Override
    public R updateUserStatus(UserDto userDto) {
        User user = BeanCopyUtils.copyBean(userDto, User.class);
        if (updateById(user)) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R deleteUserByIds(Integer[] ids) {
        if (removeByIds(Arrays.asList(ids))) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R updateUserInfo(UserDto userDto) {
        User user = BeanCopyUtils.copyBean(userDto, User.class);
        if (updateById(user)) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R getUserInfoById(Integer id) {
        User user = getById(id);
        if (user != null) {
            return R.success(user);
        }
        return R.error();
    }

    @Override
    public R addUser(UserDto userDto) {
        User user = BeanCopyUtils.copyBean(userDto, User.class);
        if (save(user)) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R login(User user) {
        if (user == null){
            return R.error("请输入用户名和密码");
        }
        if (user.getUsername() == null || user.getPassword() == null || user.getUsername().equals("") || user.getPassword().equals("")){
            return R.error("请输入用户名和密码");
        }
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        lambdaQueryWrapper.eq(User::getPassword, user.getPassword());
        //正常用户
        lambdaQueryWrapper.eq(User::getStatus, 0);
        //是管理员用户
        User one = getOne(lambdaQueryWrapper);
        if (one == null){
            return R.error("用户名或密码错误");
        }
        if (one.getStatus().equals(1)){
            return R.error("您已被禁用，请联系管理员");
        }
        if (!one.getRoleId().equals(user.getRoleId())){
            return R.error("用户名或密码错误");
        }
        //成功
        String token = JwtUtil.createJWT(String.valueOf(one.getId()));
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(one.getUsername());
        loginUser.setAvatar(one.getAvatar());
        loginUser.setJwt(token);
        return R.success(loginUser);
    }

    @Override
    public R logout() {
        return R.success("退出登录");
    }

    @Override
    public R updateUserInfo(User user) {
        Integer userId = ThreadLocalUtil.getLocal();
        user.setId(userId);
        boolean b = updateById(user);
        if (b){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R updatePassword(UpdatePasswordDto updatePasswordDto) {
        if (updatePasswordDto.getNewPassword().equals(updatePasswordDto.getOldPassword())){
            return R.error("新密码不能与旧密码相同");
        }
        Integer userId = ThreadLocalUtil.getLocal();
        User user = getById(userId);
        //判断旧密码是否正确
        if(!user.getPassword().equals(updatePasswordDto.getOldPassword())){
            return R.error("旧密码不正确");
        }
        user.setPassword(updatePasswordDto.getNewPassword());
        if (updateById(user)){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R register(User user) {
        if (user.getUsername() == null || user.getPassword() == null || user.getUsername().equals("") || user.getPassword().equals("")){
            return R.error("请输入用户名和密码");
        }
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User one = getOne(lambdaQueryWrapper);
        if (one != null){
            return R.error("用户名已存在");
        }
        //设置为普通用户
        user.setRoleId(2);
        boolean save = save(user);
        if (save){
            return R.success("注册成功");
        }
        return R.error("注册失败");
    }

}


